package com.example.liuxing.android.activity.main;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.liuxing.android.R;
import com.example.liuxing.android.activity.config.Constant;
import com.example.liuxing.android.activity.utils.SPUtils;

/**
 * Created by liuxing on 2017/6/21.
 */

public class HomeActivity extends Activity {

    private ImageView iv_home_logo;
    private ImageView iv_home_setting;
    private GridView gv_home;
    private static final String names[] = {
            "手机防盗", "通讯卫士", "软件管家", "进程管理",
            "流量统计", "病毒查杀", "缓存清理", "高级工具"};
    private static final String desc[] = {
            "手机丢失好找", "防骚扰监听", "方便管理软件", "保存手机畅通",
            "注意流量超标", "手机安全管家", "手机快步如飞", "特性处理更好"};
    private static final int icons[] = {
            R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d,
            R.drawable.e, R.drawable.f, R.drawable.h, R.drawable.j};

    private AlertDialog dialog;
    private TextView titleTextView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        initData();
        initEvent();
    }

    private void initView() {
        setContentView(R.layout.home_avtivity);
        iv_home_logo = (ImageView)findViewById(R.id.iv_home_logo);
        iv_home_setting = (ImageView)findViewById(R.id.iv_home_setting);
        gv_home = (GridView)findViewById(R.id.gv_home);
    }

    private void initData() {
        HomeAdapter homeAdapter = new HomeAdapter();
        gv_home.setAdapter(homeAdapter);
    }

    // 事件
    private void initEvent() {
        iv_home_setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(HomeActivity.this, SettingActivity.class);
                startActivity(intent);
            }
        });

        // gridView的点击事件
        gv_home.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        // 手机防盗
                        String password = SPUtils.getString(getApplicationContext(), Constant.PASSWORD);
                        if (TextUtils.isEmpty(password)) {
                            // 没有设置过密码, 设置密码对话框
                            showSetPasswordDialog();
                        } else {
                            // 设置过密码，弹出输入密码框
                            showEnterPasswordDialog();
                        }
                        break;
                    case 1:
                        Intent intent = new Intent(HomeActivity.this, Setup1Activity.class);
                        startActivity(intent);
                        break;
                }
            }
        });
    }

    private void showEnterPasswordDialog() {}

    private void showSetPasswordDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(HomeActivity.this);
        View view = View.inflate(getApplicationContext(), R.layout.password_alert, null);
        // 拿出控件
        final EditText et_password_setting = (EditText)view.findViewById(R.id.et_password_setting);
        final EditText et_password_confirm = (EditText)view.findViewById(R.id.et_password_confirm);
        Button btn_cancel = (Button)view.findViewById(R.id.btn_cancel);
        Button btn_confirm = (Button)view.findViewById(R.id.btn_confirm);
        // 设置点击事件
        // 取消
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        // 确认
        btn_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String password = et_password_setting.getText().toString().trim();
                String password_confirm = et_password_confirm.getText().toString().trim();
                if (!TextUtils.isEmpty(password) && !TextUtils.isEmpty(password_confirm)) {
                    if (password.equals(password_confirm)) {
                        // 保存密码
                        SPUtils.putString(getApplicationContext(), Constant.PASSWORD, password);
                        dialog.dismiss();
                    } else {
                        Toast.makeText(HomeActivity.this, "两次密码不同", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(HomeActivity.this, "请按要求输入", Toast.LENGTH_SHORT).show();
                }
            }
        });
        builder.setView(view);
        dialog = builder.show();
    }

    class HomeAdapter extends BaseAdapter {

        // 显示item的个数
        @Override
        public int getCount() {
            return names.length;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = View.inflate(getApplicationContext(), R.layout.home_item, null);

            ImageView iv_item_icon = (ImageView)view.findViewById(R.id.iv_item_icon);
            TextView iv_item_name = (TextView)view.findViewById(R.id.iv_item_name);
            TextView iv_item_desc = (TextView)view.findViewById(R.id.iv_item_desc);

            // 赋值
            iv_item_icon.setImageResource(icons[position]);
            iv_item_name.setText(names[position]);
            iv_item_desc.setText(desc[position]);

            return view;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }
    }
}